//
//  HMStatusViewModel.swift
//  HMSlender
//
//  Created by 赵少朋赵少朋 on 2017/2/5.
//  Copyright © 2017年 ITheima. All rights reserved.
//

import UIKit

class HMStatusViewModel: NSObject {

    
    var homeModle: HMHomeModel? {
    
        didSet {
        
            memberImage = settingMembershipImageView(mbrank: homeModle?.user?.mbrank ?? 0)
            avatarImage = settingAvatarImageView(avatar: homeModle?.user?.verified ?? -1)
            sinaSource = dealSinaSource(source: homeModle?.source)
        }
    }
    //
    var memberImage: UIImage?
    //
    var avatarImage: UIImage?
    // 处理来源
    var sinaSource: String?
    
    var dealSinaTime: String? {
    
        return dealSinaTime(createDate: homeModle?.created_at)
    }
}

extension HMStatusViewModel {

    /*-如果是今年
     - 如果是今天
     - 如果当前时间和微博时间比较 赋值给s(秒数)
     如果s <= 60 显示 刚刚
     如果s > 60 && s <= 60*60 显示 x分钟前
     如果 s > 60*60 显示 x小时前
     - 如果是昨天
     - 显示的格式为 昨天 HH:mm 例如 昨天 14:14
     - 如果不是今天和昨天
     - 显示的格式为 MM-dd HH:mm 例如 02-02 02:02
     -如果不是今年
     - 显示的格式为 yyyy-MM-dd HH:mm  例如 2015-12-12 12:12
     */
    func dealSinaTime(createDate:Date? ) -> String? {
        
        guard var sinaDate = createDate else {
            return nil
            
        }
        // 拿到当前时间
//        let currentDate = Date()
        // 初始化时间格式
        let df = DateFormatter()
        
        //判断是不是今年
        if isCurrentYear(sinaDate: sinaDate) {
            //是今年
            //获取日历
            let calendar = Calendar.current
            
            
            //是不是今天
            if calendar.isDateInToday(sinaDate) {
                
                // 是今天
                // 计算出当前日期和微博日期的秒数差值 abs 绝对值
                let s = abs(sinaDate.timeIntervalSinceNow)
                // 判断
                if s <= 60 {
                    return "刚刚"
                } else if s > 60 && s <= 3600 {
                
                    
                    return "\(Int(s / 60))分钟前"
                } else {
                    
                    return "\(Int(s / 3600))小时前"
                }
                
                
            } else if calendar.isDateInYesterday(sinaDate) {
                /// 是昨天
                df.dateFormat = "昨天 HH:mm"
                
                return df.string(from: sinaDate)
            } else {
                // 不是今天也不是昨天
                df.dateFormat = "MM-dd HH:mm"
                return df.string(from: sinaDate)
            }
            
        } else {
            //不是今年
            df.dateFormat = "yyyy-MM-dd HH:mm"
            print(df.string(from: sinaDate))
            return df.string(from: sinaDate)
        }
        
    }
    // 判断是不是今年
    func isCurrentYear(sinaDate:Date) -> Bool {
        
        let currentDate = Date()
        
        let df = DateFormatter()
        df.dateFormat = "yyyy"
        
        let sinaDateStr = df.string(from: sinaDate)
        let currentStr = df.string(from: currentDate)
        
        return sinaDateStr == currentStr ? true : false
    }
    
    func dealSinaSource(source:String?) -> String? {
//        Optional("<a href=\"http://app.weibo.com/t/feed/3auC5p\" rel=\"nofollow\">皮皮时光机</a>")
        guard let s = source, s.contains("\">") else {
            return nil
        }
        
        let start = s.range(of: "\">")
        let end = s.range(of: "</a")
        
        let result = s.substring(with: start!.upperBound..<end!.lowerBound)
        
        return "来自: \(result)"
    }
    
    func settingMembershipImageView(mbrank: Int) -> UIImage? {
        
        if mbrank > 0 && mbrank <= 6  {
           
            let imgName = "common_icon_membership_level\(mbrank)"
            let img = UIImage(named: imgName)
            return img
        
        } else {
        
            return UIImage(named: "common_icon_membership")
        }
    }
    
    
    func settingAvatarImageView(avatar: Int) -> UIImage? {
        
        
        switch avatar {
            
            case 1:
                /// 认证用户
                return UIImage(named: "avatar_vip")
            case 2,3,5:
                /// 企业
                return UIImage(named: "avatar_enterprise_vip")
            case 220:
                /// 达人
                return UIImage(named: "avatar_grassroot")
            default:
                /// 没有认证
            return UIImage(named: "avatar_vgirl")
        }
    }
}
